﻿using OrderApp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace assignment6
{
    public partial class Form1 : Form
    {
        private BindingSource bindingSource = new BindingSource();
        private BindingList<OrderService> service = new BindingList<OrderService>();
        public Form1()
        {
            InitializeComponent();
            InitializeData();
        }
        private int id = -1;
        private OrderService orderService = new OrderService();
        private Order order;
        private Customer customer;
        private Product product;
        private void InitializeData()
        {
            Customer customer1 = new Customer(1, "Customer1");
            Customer customer2 = new Customer(2, "Customer2");

            Product milk = new Product(1, "Milk", 69.9f);
            Product eggs = new Product(2, "eggs", 4.99f);
            Product apple = new Product(3, "apple", 5.59f);

            Order order1 = new Order(1, customer1, new DateTime(2021, 3, 21));
            order1.AddDetails(new OrderDetail(apple, 8));
            order1.AddDetails(new OrderDetail(eggs, 10));

            Order order2 = new Order()
            {
                Id = 2,
                Customer = customer2,
                CreateTime = new DateTime(2021, 3, 21),
                Details = { new OrderDetail(eggs, 10), new OrderDetail(milk, 10) }
            };

            Order order3 = new Order(3, customer2, new DateTime(2021, 3, 21));
            order3.AddDetails(new OrderDetail(milk, 100));

            orderService.AddOrder(order1);
            orderService.AddOrder(order2);
            orderService.AddOrder(order3);
            service.Add(orderService);
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if(int.TryParse(textBox1.Text,out int num))
            {
                id = num;
            }
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            customer.Id = id;
            customer.Name = textBox2.Text;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            order = new Order(id, customer, dateTimePicker1.Value);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            product.Name = listBox1.Text;
            product.Id = id;
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if(float.TryParse(textBox5.Text,out float num2))
            {
                product.Price = num2;
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            if(int.TryParse(textBox6.Text,out int num3))
            {
                order.AddDetails(new OrderDetail(product, num3));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            orderService.AddOrder(order);
            service.Clear();
            service.Add(orderService);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            orderService.RemoveOrder(id);
            orderService.QueryAll().ForEach(
                o => Console.WriteLine(o));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Console.WriteLine(orderService.GetOrder(id));
        }
    }
}
